package com.citcc.common.jpush;

import com.alibaba.fastjson.JSONObject;
import com.citcc.common.utils.StringUtils;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import sun.misc.BASE64Encoder;

import java.util.List;

/**
 * 激光消息推送
 */
public class PushUtil {

    public static final Logger log = LoggerFactory.getLogger(PushUtil.class);

    /**
     * 1.极光推送后台APPKEY,MASTER_SECRET
     */
    private static final String APP_KEY = "a090776a89c6ca306c5f59c6";
    private static final String MASTER_SECRET = "aa263215bd07c610897bd44e";
    //true为线上环境，false为开发环境
    private static final boolean production = true;

    //1.发送通知
    private static final String SEND_NOTIFICATION_URL = "https://api.jpush.cn/v3/push";

    //提醒内部调用
    public static void msgPush(List<Object> registrationIdList, String title, String body, String type,String url,String deviceType) throws Exception {
       if (!StringUtils.isEmpty(deviceType)){
           if ("0".equals(deviceType)){//调用Android
               notificationRemindAndroid(registrationIdList, title, body, url, type, "android","","");
           }else if ("1".equals(deviceType)){//调用ios
               notificationRemindIos(registrationIdList, title, body, url, type, "ios","","");
           }
       }
    }

    //消息调用
    public static void messagePush(List<Object> registrationIdList, String title, String body, String type,String url,String deviceType) throws Exception {
       if (!StringUtils.isEmpty(deviceType)){
           if ("0".equals(deviceType)){//调用Android
               pushMessage(title, body, url,type,registrationIdList,"android");
           }else if ("1".equals(deviceType)){//调用ios
               pushMessage(title, body, url,type,registrationIdList,"ios");
           }
       }
    }


    public static void notification(List<Object> registrationIdList, String title, String body, String url, String type, String platform) throws Exception {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("url", url);
        jsonObject.put("type", type);
        JSONObject jsonEntity = new JSONObject();
        if ("ios".equalsIgnoreCase(platform)) {
            jsonEntity.put("notification", getNotificationIOS(body, title, jsonObject));

            //True 表示推送生产环境，False 表示要推送开发环境
            JSONObject options = new JSONObject();
            options.put("apns_production", production);
            jsonEntity.put("options", options);
        }
        if ("android".equalsIgnoreCase(platform)) {
            jsonEntity.put("notification", getNotificationAndroid(body, title, jsonObject));
        }
        jsonEntity.put("platform", platform);
        if (registrationIdList == null) {
            jsonEntity.put("audience", "all");
        } else {
            JSONObject jsonAudience = new JSONObject();
            jsonAudience.put("registration_id",registrationIdList);
            jsonEntity.put("audience", jsonAudience);
        }

        BASE64Encoder encoder = new BASE64Encoder();
        String base64_auth_string = encoder.encode((APP_KEY + ":" + MASTER_SECRET).getBytes());
        String authorization = "Basic " + base64_auth_string;

        HttpPost httpPost = new HttpPost(SEND_NOTIFICATION_URL);
        httpPost.addHeader("Content-Type", "application/json");
        httpPost.addHeader("Authorization", authorization);
        System.out.println(jsonEntity.toString());
        httpPost.setEntity(new StringEntity(jsonEntity.toString(), "UTF-8"));
        HttpResponse response = HttpClients.createDefault().execute(httpPost);
        String string = EntityUtils.toString(response.getEntity(), "UTF-8");
        System.out.println(string);
    }

    //提醒通知ios
    public static void notificationRemindIos(List<Object> registrationIdList, String title, String body, String url, String type, String platform,String routeId,String routeName) throws Exception {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("url", url);
        jsonObject.put("type", type);
        jsonObject.put("routeId",routeId);
        jsonObject.put("routeName",routeName);
        JSONObject jsonEntity = new JSONObject();
        jsonEntity.put("notification", getNotificationIOS(body, title, jsonObject));
        //True 表示推送生产环境，False 表示要推送开发环境
        JSONObject options = new JSONObject();
        options.put("apns_production", production);
        jsonEntity.put("options", options);
        jsonEntity.put("platform", platform);
        if (registrationIdList == null) {
            jsonEntity.put("audience", "all");
        } else {
            JSONObject jsonAudience = new JSONObject();
            jsonAudience.put("registration_id",registrationIdList);
            jsonEntity.put("audience", jsonAudience);
        }

        BASE64Encoder encoder = new BASE64Encoder();
        String base64_auth_string = encoder.encode((APP_KEY + ":" + MASTER_SECRET).getBytes());
        String authorization = "Basic " + base64_auth_string;

        HttpPost httpPost = new HttpPost(SEND_NOTIFICATION_URL);
        httpPost.addHeader("Content-Type", "application/json");
        httpPost.addHeader("Authorization", authorization);
        httpPost.setEntity(new StringEntity(jsonEntity.toString(), "UTF-8"));
        HttpResponse response = HttpClients.createDefault().execute(httpPost);
        String string = EntityUtils.toString(response.getEntity(), "UTF-8");
        System.out.println(string);
    }

    //提醒通知Android
    public static void notificationRemindAndroid(List<Object> registrationIdList, String title, String body, String url, String type, String platform,String routeId,String routeName) throws Exception {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("url", url);
        jsonObject.put("type", type);
        jsonObject.put("routeId",routeId);
        jsonObject.put("routeName",routeName);
        JSONObject jsonEntity = new JSONObject();
        jsonEntity.put("notification", getNotificationAndroid(body, title, jsonObject));
        //True 表示推送生产环境，False 表示要推送开发环境
        JSONObject options = new JSONObject();
        options.put("apns_production", production);
        jsonEntity.put("options", options);
        jsonEntity.put("platform", platform);
        if (registrationIdList == null) {
            jsonEntity.put("audience", "all");
        } else {
            JSONObject jsonAudience = new JSONObject();
            jsonAudience.put("registration_id",registrationIdList);
            jsonEntity.put("audience", jsonAudience);
        }

        BASE64Encoder encoder = new BASE64Encoder();
        String base64_auth_string = encoder.encode((APP_KEY + ":" + MASTER_SECRET).getBytes());
        String authorization = "Basic " + base64_auth_string;

        HttpPost httpPost = new HttpPost(SEND_NOTIFICATION_URL);
        httpPost.addHeader("Content-Type", "application/json");
        httpPost.addHeader("Authorization", authorization);
        httpPost.setEntity(new StringEntity(jsonEntity.toString(), "UTF-8"));
        HttpResponse response = HttpClients.createDefault().execute(httpPost);
        String string = EntityUtils.toString(response.getEntity(), "UTF-8");
        System.out.println(string);
    }

    //消息调用
    public static void pushMessage(String title, String body, String url, String type,List<Object> registrationIdList, String platform) throws Exception {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("url", url);
        jsonObject.put("type", type);
        JSONObject jsonEntity = new JSONObject();
        jsonEntity.put("message", getMessage(body, title, jsonObject));
        jsonEntity.put("platform", platform);
        if (registrationIdList == null) {
            jsonEntity.put("audience", "all");
        } else {
            JSONObject jsonAudience = new JSONObject();
            jsonAudience.put("registration_id",registrationIdList);
            jsonEntity.put("audience", jsonAudience);
        }

        BASE64Encoder encoder = new BASE64Encoder();
        String base64_auth_string = encoder.encode((APP_KEY + ":" + MASTER_SECRET).getBytes());
        String authorization = "Basic " + base64_auth_string;

        HttpPost httpPost = new HttpPost(SEND_NOTIFICATION_URL);
        httpPost.addHeader("Content-Type", "application/json");
        httpPost.addHeader("Authorization", authorization);
        httpPost.setEntity(new StringEntity(jsonEntity.toString(), "UTF-8"));
        HttpResponse response = HttpClients.createDefault().execute(httpPost);
        String string = EntityUtils.toString(response.getEntity(), "UTF-8");
        System.out.println(string);
    }

    private static JSONObject getMessage(String body, String title, JSONObject extrasParam) {
        JSONObject message = new JSONObject();
        message.put("msg_content", body);
        message.put("title", title);
        message.put("extras", extrasParam);
        return message;
    }


    /**
     * 通知封装IOS
     */
    private static JSONObject getNotificationIOS(String body, String title, JSONObject extrasParam) {
        JSONObject notification = new JSONObject();
        notification.put("ios", IOS_Notification(body, title, extrasParam));
        return notification;
    }

    /**
     * 通知封装android
     */
    private static JSONObject getNotificationAndroid(String body, String title, JSONObject extrasParam) {
        JSONObject notification = new JSONObject();
        notification.put("android", android_Notification(body, title, extrasParam));
        return notification;
    }

    /**
     * 安卓通知封装
     */
    private static JSONObject android_Notification(String body, String title, JSONObject extrasParam) {
        JSONObject android = new JSONObject();             //3.1 android通知内容
        android.put("alert", body);                      //设置通知内容
        android.put("title", title);                      //设置通知内容
        android.put("extras", extrasParam);
        return android;
    }

    /**
     * 苹果通知封装
     */
    private static JSONObject IOS_Notification(String body, String title, JSONObject extrasParam) {
        JSONObject ios = new JSONObject();//3.2 ios通知内容
        ios.put("sound", "default");
        //ios.put("badge", "+1");

        //消息体封装
        JSONObject iosNotify = new JSONObject();
        iosNotify.put("title", title);
        iosNotify.put("body", body);
        ios.put("alert", iosNotify);
        ios.put("extras", extrasParam);
        return ios;
    }


}